<?php
/**
 * User: mzh03@163.com
 * Date: 2016/8/25
 * Time: 下午 05:28
 */


namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class SchoolController extends AdminBaseController {

    // 定义数据表
    private $db;

    // 构造函数 实例化SchoolinfoModel表
    public function __construct(){
        parent::__construct();
        $this->db=D('Schoolinfo');
    }

    // 学校列表
    public function index(){
        // 查询记录总录数
        $count = $this->db->count();
        $Page = getpage($count, 10);

        $show = $Page->show();
        $data=D('Schoolinfo')->join('bjyadmin_region ON bjyadmin_schoolinfo.r_id = bjyadmin_region.id')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($data as $k => $v){
//            echo $v['schoolassociation'].'<br>';
            $cnt = count(explode(',', $v['schoolassociation']));
            if($v['schoolassociation']){
                if($cnt ==1){
                    $data[$k]['cou']=1;
                }else{
                    $data[$k]['cou'] = $cnt;
                }
            } else{
                $data[$k]['cou']=0;
            }
        }
//exit;
        $assign=array(
            'data'=>$data,
            'page'=>$show
        );
        $this->assign($assign);
        $this->display();
    }

    //学校添加
    public function add(){
        if(IS_POST){

            if($aid=$this->db->addData()){
                $this->success('学校添加成功',U('Admin/School/index'));
            }else{
                $this->error($this->db->getError());
            }
        } else{
            $data=D('Region')->select();
            $assign=array(
                'data'=>$data
            );
            $this->assign($assign);
            $this->display();
        }
    }

    // 彻底删除
    public function delete(){
        if($this->db->deleteData()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 修改学校
     */
    public function edit(){
        if(IS_POST){
            if($this->db->editData()){
                $this->success('修改成功',U('Admin/School/index'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $sid=I('get.sid',0,'intval');
            // 获取学校数据
            $school_data=M('Schoolinfo')->find($sid);
            // 获取相对路径的图片地址
            $school_data['content_facilities']=preg_ueditor_image_path($school_data['content_facilities']);
            $school_data['content_achievement']=preg_ueditor_image_path($school_data['content_achievement']);
            $school_data['content_spirit']=preg_ueditor_image_path($school_data['content_spirit']);
            $school_data['content_admin']=preg_ueditor_image_path($school_data['content_admin']);
            // 获取区域
            $data=D('Region')->select();
            // 获取学校相关
            $cdata = D('Schoolinfo')->where(array('schoolcate'=>$school_data['schoolcate']))->field('sid,schoolname')->select();
            // 获取选中学校相关
            $aso_data = explode(',', $school_data['schoolassociation']);
            $assign=array(
                'school_data'=>$school_data,
                'data'=>$data,
                'cdata'=>$cdata,
                'aso_data'=>$aso_data
            );
            $this->assign($assign);
            $this->display();
        }
    }

    /**
     * ajax返回学校
     */
    public function showSchool(){
        $shl = I('post.schoolcate');
        $w['schoolcate'] = $shl;
        $cdata = D('Schoolinfo')->where($w)->select();
        $info='';
        foreach ($cdata as $row) {
            $info .= '<input type="checkbox" name="category[]" value="'.$row['sid'].'" />&nbsp;'.$row['schoolname'].'&nbsp;&nbsp;';
        }
        $this->ajaxReturn($info);
    }
}